package com.skydoves.preferenceroomdemo;

import com.skydoves.preferenceroomdemo.components.PreferenceComponent_AppComponent;
import com.skydoves.preferenceroomdemo.entities.Preference_UserProfile;
import com.skydoves.preferenceroomdemo.models.PrivateInfo;
import com.skydoves.preferenceroom.InjectPreference;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class LoginAbility extends Ability {
    private static final HiLogLabel label =
            new HiLogLabel(HiLog.LOG_APP, 0x00201, "LoginAbility");

    @InjectPreference
    public Preference_UserProfile userProfile;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_login);
        PreferenceComponent_AppComponent.getInstance().inject(this);

        TextField editTextNick = null;
        Component cpt1 =
                findComponentById(ResourceTable.Id_login_editText_nick);
        if (cpt1 instanceof TextField) {
            editTextNick = (TextField) cpt1;
        }
        TextField editTextAge = null;
        Component cpt2 =
                findComponentById(ResourceTable.Id_login_editText_age);
        if (cpt2 instanceof TextField) {
            editTextAge = (TextField) cpt2;
        }
        Button button = null;
        Component cpt3 =
                findComponentById(ResourceTable.Id_login_button);
        if (cpt3 instanceof Button) {
            button = (Button) cpt3;
        }
        TextField finalEditTextNick = editTextNick;
        TextField finalEditTextAge = editTextAge;
        button.setClickedListener(component -> {
            String inputNick = finalEditTextNick.getText();
            String inputAge = finalEditTextAge.getText();
            if (!inputNick.equals("") && !inputAge.equals("")) {
                userProfile.putLogin(true);
                userProfile.putNickname(inputNick);
                userProfile.putUserinfo(new
                        PrivateInfo(inputNick, toInt(inputAge)));

                jumpMain();
                terminateAbility();
            } else {
                new ToastDialog(getApplicationContext())
                        .setText("please fill all inputs")
                        .show();
            }
        });
        userProfile.addNicknameOnChangedListener(
                nickname -> {
                    new ToastDialog(getApplicationContext())
                            .setText("onChanged :" + nickname)
                            .show();
                });
    }

    private void jumpMain() {
        Intent intent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.skydoves.preferenceroomdemo")
                .withAbilityName(MainAbility.class.getName())
                .build();
        intent.setOperation(operation);
        startAbility(intent);
        terminateAbility();
    }

    private int toInt(String value) {
        int result = 0;
        try {
            result = Integer.parseInt(value);
        } catch (NumberFormatException e) {
            HiLog.error(label, e.getMessage());
        }
        return result;
    }

}